
using System.Windows.Navigation;
using AnalyStock.DataManage;

namespace AnalyStock.WPFPage;

public partial class NavigationPage : Page
{
    private ClassifyStockPage classifyStockPage;
    private LoadDataPage loadDataPage;
    private LookStockPage lookStockPage;
    private SelectStockPage selectStock;
    private RealTimePage realTimePage;
    private TitlePage titlePage;
    private readonly Button[] btnArry;
    public NavigationPage()
    {
        InitializeComponent();
        btnArry =
        [
            BtnGoBack,
            BtnLoadData,
            BtnClassifyStock,
            BtnLookStock,
            BtnRealTime,
            BtnSelectStock,
            BtnExit
        ];
        btnArry.ForEach(n =>
                n.Click += BtnLoadChilderPage_Click);
        LoadPages(ref titlePage);

    }

    private void BtnLoadChilderPage_Click(object sender, RoutedEventArgs e)
    {
        var currBtn = sender as Button;
        ReTextBtnContent(currBtn);
        switch (currBtn)
        {
            case { Name: nameof(BtnLoadData) }:
                LoadPages(ref loadDataPage);
                break;
            case { Name: nameof(BtnLookStock) }:
                LoadPages(ref lookStockPage);
                break;
            case { Name: nameof(BtnClassifyStock) }:
                LoadPages(ref classifyStockPage);
                break;
            case { Name: nameof(BtnRealTime) }:
                LoadPages(ref realTimePage);
                break;
            case { Name: nameof(BtnSelectStock) }:
                LoadPages(ref selectStock);
                break;
            case { Name: nameof(BtnGoBack) }:
                LoadPages(ref titlePage);
                break;
            case { Name: nameof(BtnExit) }:
                ExitApp();
                break;
        }

    }

    private void ReTextBtnContent(Button currBtn)
    {
        btnArry.ForEach(n =>
            n.Content = "[  ] " + n.Content.ToString().Split("] ").Last());
        currBtn.Content = "[●] " + currBtn.Content.ToString().Split("] ").Last();
    }



    private void ExitApp()
    {
        (Parent as NavigationWindow).Close();
    }

    private void LoadPages<T>(ref T t) where T : Page, new()
    {
        t ??= new T();
        MainFrame.NavigationService.Navigate(t);
    }



}